﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using DBService;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using YArchitech.Revit.MEP.Opening.Properties;

namespace YArchitech.Revit.MEP.Opening
{
	public partial class OfferDataConfigForm : System.Windows.Forms.Form
	{
		public OfferDataConfigForm()
		{
			this.InitializeComponent();
		}

		private void btnSteelDrivepipeSize_Click(object sender, EventArgs e)
		{
			new SteelDrivepipeSizeTableForm().ShowDialog();
		}

		private void OfferDataConfigForm_Load(object sender, EventArgs e)
		{
			base.Icon = Resource.YArchitechIcon;
			string strXmlFilePath = ConfigDataOperate.InitConfigDataFile();
			this.m_cfgData = new ConfigDataOperate.OfferConfigData(strXmlFilePath);
			this.UpdateCtrlDataShow();
		}

		private void SelbtnServerSel_Click(object sender, EventArgs e)
		{
			new DlgConfig().ShowDialog();
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			if (string.IsNullOrEmpty(this.m_cfgData.LocalOperDataFilePath))
			{
				this.m_cfgData.LocalOperDataFilePath = Product.UserDataLocation + "\\SynergyData.xml";
			}
			this.m_cfgData.CircelOpeningDist2Pipe = ConfigDataOperate.ConvertString2Double(this.txtDistCircleOpening2Pipe.Text);
			this.m_cfgData.RectOpeningDist2Pipe = ConfigDataOperate.ConvertString2Double(this.txtDistRectOpening2Pipe.Text);
			this.m_cfgData.MergeOpeningMaxDist = ConfigDataOperate.ConvertString2Double(this.txtMergeOpeningMaxDist.Text);
			this.m_cfgData.MergeOpeningExtend = ConfigDataOperate.ConvertString2Double(this.txtMergeOpeningExtend.Text);
			this.m_cfgData.WaterProofTubleDefType = this.GetWaterProofTubeType();
			this.m_cfgData.FlexWPDrivePipeDimType = (ConfigDataOperate.EWaterProofFlexiblePipeDimPart)this.cmbFlexWPDimPart.SelectedIndex;
			this.m_cfgData.InFlexWPDrivePipeDimType = (ConfigDataOperate.EWaterProofInflexiblePipeDimPart)this.cmbInFlexWPDimPart.SelectedIndex;
			this.m_cfgData.FlexSealDrivePipeDimType = (ConfigDataOperate.ESealFlexiblePipeDimPart)this.cmbFlexSealDimPart.SelectedIndex;
			this.m_cfgData.InFlexSealDrivePipeDimType = (ConfigDataOperate.ESealInflexiblePipeDimPart)this.cmbInFlexSealDimPart.SelectedIndex;
			this.m_cfgData.WriteCfgData2Xml();
			FileInfo fileInfo = new FileInfo(this.m_cfgData.LocalOperDataFilePath);
			if (!fileInfo.Exists)
			{
				try
				{
					fileInfo.Create();
				}
				catch
				{
					YJKMessageBox.Show("创建提资文件失败!");
				}
			}
		}

		private ConfigDataOperate.EDrivePipeType GetWaterProofTubeType()
		{
			if (this.radioBtnFlexB.Checked)
			{
				return ConfigDataOperate.EDrivePipeType.eWaterProofFlexibleB;
			}
			if (this.radioBtnInflexA.Checked)
			{
				return ConfigDataOperate.EDrivePipeType.eWaterProofInflexibleA;
			}
			if (this.radioBtnInflexB.Checked)
			{
				return ConfigDataOperate.EDrivePipeType.eWaterProofInflexibleB;
			}
			if (this.radioBtnInflexC.Checked)
			{
				return ConfigDataOperate.EDrivePipeType.eWaterProofInflexibleC;
			}
			return ConfigDataOperate.EDrivePipeType.eWaterProofFlexibleA;
		}

		private void UpdateCtrlDataShow()
		{
			this.txtDistCircleOpening2Pipe.Text = this.m_cfgData.CircelOpeningDist2Pipe.ToString("F0");
			this.txtDistRectOpening2Pipe.Text = this.m_cfgData.RectOpeningDist2Pipe.ToString("F0");
			this.txtMergeOpeningMaxDist.Text = this.m_cfgData.MergeOpeningMaxDist.ToString("F0");
			this.txtMergeOpeningExtend.Text = this.m_cfgData.MergeOpeningExtend.ToString("F0");
			this.radioBtnFlexA.Checked = (this.m_cfgData.WaterProofTubleDefType == ConfigDataOperate.EDrivePipeType.eWaterProofFlexibleA);
			this.radioBtnFlexB.Checked = (this.m_cfgData.WaterProofTubleDefType == ConfigDataOperate.EDrivePipeType.eWaterProofFlexibleB);
			this.radioBtnInflexA.Checked = (this.m_cfgData.WaterProofTubleDefType == ConfigDataOperate.EDrivePipeType.eWaterProofInflexibleA);
			this.radioBtnInflexB.Checked = (this.m_cfgData.WaterProofTubleDefType == ConfigDataOperate.EDrivePipeType.eWaterProofInflexibleB);
			this.radioBtnInflexC.Checked = (this.m_cfgData.WaterProofTubleDefType == ConfigDataOperate.EDrivePipeType.eWaterProofInflexibleC);
			this.cmbFlexWPDimPart.Items.Clear();
			for (int i = 0; i < 5; i++)
			{
				this.cmbFlexWPDimPart.Items.Add(ConfigDataOperate.arDrivePipeDimPartName[i]);
			}
			this.cmbFlexWPDimPart.SelectedIndex = (int)this.m_cfgData.FlexWPDrivePipeDimType;
			this.cmbInFlexWPDimPart.Items.Clear();
			for (int j = 0; j < 4; j++)
			{
				this.cmbInFlexWPDimPart.Items.Add(ConfigDataOperate.arDrivePipeDimPartName[j]);
			}
			this.cmbInFlexWPDimPart.SelectedIndex = (int)this.m_cfgData.InFlexWPDrivePipeDimType;
			this.cmbFlexSealDimPart.Items.Clear();
			for (int k = 0; k < 7; k++)
			{
				this.cmbFlexSealDimPart.Items.Add(ConfigDataOperate.arDrivePipeDimPartName[k]);
			}
			this.cmbFlexSealDimPart.SelectedIndex = (int)this.m_cfgData.FlexSealDrivePipeDimType;
			this.cmbInFlexSealDimPart.Items.Clear();
			for (int l = 0; l < 4; l++)
			{
				this.cmbInFlexSealDimPart.Items.Add(ConfigDataOperate.arDrivePipeDimPartName[l]);
			}
			this.cmbInFlexSealDimPart.SelectedIndex = (int)this.m_cfgData.InFlexSealDrivePipeDimType;
		}

		private void UpdateCtrlStatus()
		{
		}

		private void radioButtonLocal_CheckedChanged(object sender, EventArgs e)
		{
			this.UpdateCtrlStatus();
		}

		private void radioButtonServer_CheckedChanged(object sender, EventArgs e)
		{
			this.UpdateCtrlStatus();
		}

		private ConfigDataOperate.OfferConfigData m_cfgData;
	}
}
